﻿using System;
using System.Collections.Generic;
using System.Linq;
using si.caha.Model;

namespace si.caha.Repository
{
    public class UserRepository : BaseRepository
    {
        public UserRepository(DataContext dataConext) : base(dataConext){}

        public User Get(int id)
        {
            return GetDataContext().Users.FirstOrDefault(u => u.UserId == id);
        }

        public List<User> GetAll()
        {
            return GetDataContext().Users.ToList();
        }

        public bool DoesUserWithEmailExists(string email)
        {
            return GetDataContext().Users.Where(u => u.Email == email).Any();
        }

        public bool DoesUserHaveAddress(string email)
        {
            return GetDataContext().Users.Where(u => u.Email == email && u.Addresses.Count > 0).Any();
        }

        // registration is specific form because of WebSecurity helper...
        public Dictionary<string, string> Add(User entity)
        {
            Dictionary<string, string> errors = ValidateAndGetErrors(entity);
            if (errors.Count == 0)
            {
                GetDataContext().Users.Add(entity);
                GetDataContext().SaveChanges();
            }
            return errors;
        }
    }
}